package cn.codeforfun.initialization.server.annotation.impl;

import cn.codeforfun.initialization.server.ServerService;
import cn.codeforfun.initialization.server.annotation.UniqueHost;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 自定义host校验
 *
 * @author wangbin
 */
@Component
public class UniqueHostValidator implements ConstraintValidator<UniqueHost, String> {
    @Resource
    private ServerService serverService;

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        return value == null || !serverService.isServerHostExist(value);
    }
}
